

#ifndef __LK8563_H__
#define __LK8563_H__

#ifdef __cplusplus
extern "C" {
#endif

#define PCF8563_I2C_ADDR                0x51
#define PCF8563_READ_ADDR               0xA3
#define PCF8563_WRITE_ADDR              0xA2

/*
0: RTC正常
1: RTC未校准
2: RTC异常
*/
typedef enum {
	LK8563_STATUS_OK = 0,
	LK8563_STATUS_NOT_CALIBRATED,
	LK8563_STATUS_ERROR
}lk8563_status_t;

typedef struct {
    uint8_t minute;
    uint8_t hour;
    uint8_t day;
    uint8_t weekday;
} PCF_Alarm;

typedef struct {
    uint8_t second;
    uint8_t minute;
    uint8_t hour;
    uint8_t day;
    uint8_t weekday;
    uint8_t month;
    uint16_t year;
} PCF_DateTime;

typedef struct {
    uint8_t dev_addr;
    uint32_t clk_speed;
    // uint8_t addr_wordlen;               /*!< block address wordlen */
    // uint8_t write_time_ms;              /*!< lk8563 write time, typically 10ms*/
} i2c_lk8563_config_t;

typedef struct i2c_lk8563 {
    void* i2c_dev;      /*!< I2C device handle */
    uint8_t addr_wordlen;                 /*!< block address wordlen */
    uint8_t *buffer;                      /*!< I2C transaction buffer */
    uint8_t write_time_ms;                /*!< I2C lk8563 write time(ms)*/
} i2c_lk8563_t;

/* handle of lk8563 device */
typedef struct i2c_lk8563 *i2c_lk8563_handle_t;

extern i2c_lk8563_handle_t i2c_dev_lk8563;

esp_err_t PCF_Init(void * bus_handle, const i2c_lk8563_config_t *lk8563_config, i2c_lk8563_handle_t *lk8563_handle);

esp_err_t PCF_Write(i2c_lk8563_handle_t handle, uint8_t addr, uint8_t *data, size_t count);
esp_err_t PCF_Read(i2c_lk8563_handle_t handle, uint8_t addr, uint8_t *data, size_t count);
esp_err_t PCF_GetLastError();
int PCF_GetAndClearFlags(i2c_lk8563_handle_t handle);
int PCF_SetClockOut(i2c_lk8563_handle_t handle, uint8_t mode);
int PCF_SetTimer(i2c_lk8563_handle_t handle, uint8_t mode, uint8_t count);
int PCF_GetTimer(i2c_lk8563_handle_t handle);
int PCF_SetAlarm(i2c_lk8563_handle_t handle, PCF_Alarm *alarm);
int PCF_GetAlarm(i2c_lk8563_handle_t handle, PCF_Alarm *alarm);
int PCF_SetDateTime(i2c_lk8563_handle_t handle, PCF_DateTime *dateTime);
int PCF_GetDateTime(i2c_lk8563_handle_t handle, PCF_DateTime *dateTime);
/*
	同步pcf8563时钟到系统时间
	return 0:success
	return 1:clock integrity not guaranted
	return -1:i2c error
*/
int PCF_hctosys();
int PCF_systohc();

#endif /* __LK8563_H__ */


#ifdef __cplusplus
}
#endif


